package com.zqxx.examine.management.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.management.persistence.TrainingMapper;
import com.zqxx.examine.model.training.Training;
import com.zqxx.examine.model.training.TrainingSection;
import com.zqxx.examine.model.training.TrainingSectionProcess;
import com.zqxx.examine.model.training.UserTraining;

@Service("trainingService")
public class TrainingService
{
  @Autowired
  private TrainingMapper trainingMapper;
  
  public List<Training> getTrainingList(int userId, Page<Training> page)
  {
    return this.trainingMapper.getTrainingList(userId, page);
  }
  
  public void addTraining(Training training)
  {
    this.trainingMapper.addTraining(training);
  }
  
  public List<TrainingSection> getTrainingSectionByTrainingId(int trainingId, int userId, Page<TrainingSection> page)
  {
    return this.trainingMapper.getTrainingSectionByTrainingId(trainingId, userId, page);
  }
  
  public List<TrainingSection> getTrainingSectionById(int sectionId, int userId, Page<TrainingSection> page)
  {
    return this.trainingMapper.getTrainingSectionById(sectionId, userId, page);
  }
  
  public void deleteTrainingSection(int sectionId, int userId)
  {
    this.trainingMapper.deleteTrainingSection(sectionId, userId);
  }
  
  public void addTrainingSection(TrainingSection section)
  {
    this.trainingMapper.addTrainingSection(section);
  }
  
  public Map<Integer, List<TrainingSectionProcess>> getTrainingSectionProcessMapByTrainingId(int trainingId, List<Integer> idList, String searchStr)
  {
    if (idList.size() == 0) {
      idList = null;
    }
    List<TrainingSectionProcess> processList = this.trainingMapper.getTrainingSectionProcessListByTrainingId(trainingId, idList, searchStr, null);
    HashMap<Integer, List<TrainingSectionProcess>> map = new HashMap();
    for (TrainingSectionProcess process : processList)
    {
      List<TrainingSectionProcess> tmpList = new ArrayList();
      if (map.containsKey(Integer.valueOf(process.getUserId()))) {
        tmpList = (List)map.get(Integer.valueOf(process.getUserId()));
      }
      tmpList.add(process);
      map.put(Integer.valueOf(process.getUserId()), tmpList);
    }
    return map;
  }
  
  public List<UserTraining> getUserTrainingList(int trainingId, int userId, String searchStr, Page<UserTraining> page)
  {
    return this.trainingMapper.getUserTrainingList(trainingId, userId, searchStr, page);
  }
  
  public void deleteTraining(int trainingId, int userId)
  {
    this.trainingMapper.deleteTraining(trainingId, userId);
  }
}
